package com.wxz.gblw.cjg.service;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wxz.gblw.cjg.common.base.BaseWxzService;
import com.wxz.gblw.cjg.common.model.BeanOperation;
import com.wxz.gblw.cjg.common.model.RequestFilters;
import com.wxz.gblw.cjg.entity.Knowledge;
import com.wxz.gblw.cjg.mapper.KnowledgeMapper;
import com.wxz.gblw.cjg.vo.KnowledgeVo;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Knowledge
 * @author xiangzhong.wang
 * @date 2019-12-31 15:17:33
 */
@Service
public class KnowledgeService extends BaseWxzService<KnowledgeMapper, Knowledge> {

    public boolean saveEntity(KnowledgeVo voEntity) {
        Knowledge poEntity = new Knowledge();
        BeanCopier copier = BeanCopier.create(KnowledgeVo.class, Knowledge.class,
                false);

        poEntity.setCreateTime(new Date());

        copier.copy(voEntity, poEntity, null);
        return save(poEntity);
    }

    public Object getDetails(Long id) {
        return this.getById(id);
    }

    public boolean editEntity(KnowledgeVo voEntity) {
        Knowledge poEntity = getById(voEntity.getId());
        if(null == poEntity){
            //throw new ServiceLayerException(10001222,"计划类型或已被其他用户删除，请刷新后重试！！！");
        }

//        Map<String,Object> columnMap = new HashMap<>();
//        columnMap.put("year",voEntity.getYear());
//        columnMap.put("title",voEntity.getTitle());
//        List<PlanType> otList = (List<PlanType>) this.listByMap(columnMap);
//        if(otList!=null && !otList.isEmpty() && otList.get(0).getId() != voEntity.getId()){
//            throw new ServiceLayerException(10001222,"该年度计划类型标题已存在，请修改！！！");
//        }

        BeanCopier copier = BeanCopier.create(KnowledgeVo.class, Knowledge.class,
                false);
        copier.copy(voEntity, poEntity, null);
        return this.saveOrUpdate(poEntity);
    }

    public boolean deleteEntity(Long id) {
        return removeById(id);
    }

    public Object queryEntitiesByPage(List<RequestFilters> params, long knowledgeTypeId, long currentPage, long pageSize) throws Exception {
        /** 实例化查询构造器*/
        QueryWrapper<Knowledge> wrapper = new QueryWrapper<>();
        /** 选择查询列*/
        wrapper.select("id,name,view_order as viewOrder,code,create_time AS createTime,remark,logo").orderByAsc("view_order");
        /** 构建查询sql*/
        wrapper.eq("fk_knowledge_type_id", knowledgeTypeId);
        BeanOperation.comboQueryConditions(wrapper,params);
        IPage<Map<String,Object>> pageMaps = pageMaps(new Page<>(currentPage, pageSize),wrapper);
        return pageMaps;
    }
}
